package com.yixing.tech.wms.statistics.stream;

import com.yixing.tech.wms.statistics.domain.entity.InventoryInitialization;
import com.yixing.tech.wms.statistics.service.IInventoryOperationFlowStatisticsService;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import com.mongodb.client.model.changestream.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.data.mongodb.core.messaging.Message;
import org.springframework.data.mongodb.core.messaging.MessageListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class InventoryInitializationListener implements MessageListener<ChangeStreamDocument<Document>, InventoryInitialization> {

    @Resource
    IInventoryOperationFlowStatisticsService inventoryOperationFlowStatisticsService;

    @Override
    public void onMessage(Message<ChangeStreamDocument<Document>, InventoryInitialization> message) {
        OperationType operationType = message.getRaw().getOperationType();
        log.error("操作类型:{}", operationType);
        log.error("变更数据主体:{}", message.getBody());

        if(OperationType.UPDATE.equals(operationType)) {
            inventoryOperationFlowStatisticsService.insertInventoryOperationFlowFromInventoryInitialization(message.getBody());
        }
    }
}
